package es.ojugames.yincana.creacion;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

import es.ojugames.yincana.R;



public class SeleccionarUbicacionActivity extends MapActivity {
	private MapView mapa;
	private MapController mapController;
	//private boolean premik;
	
	private List<Overlay> mapOverlays;
	private MyItemizedOverlay itemizedoverlay;
	private Drawable marker;
	
	private TextView longitudText;
	private TextView latitudText;
	
	Double longitud;
	Double latitud;
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// fullscreen
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.seleccionar_ubicacion);
		
		mapa = (MapView) this.findViewById(R.id.mapa);
		
		longitudText = (TextView) this.findViewById(R.id.longitud);
		latitudText = (TextView) this.findViewById(R.id.latitud);
				
		mapController = mapa.getController();
		
		LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		Location loc1 = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		
		Double longitudObtenida = loc1.getLongitude() * 1E6;
		Double latitudObtenida = loc1.getLatitude() * 1E6;
		
		GeoPoint loc2 = new GeoPoint(latitudObtenida.intValue(), longitudObtenida.intValue());
		
		mapController.setCenter(loc2);
		mapController.setZoom(18);
		mapa.setSatellite(true);
		
		mapOverlays = mapa.getOverlays();
		marker = this.getResources().getDrawable(R.drawable.marker);
		
		//premik = false;
		
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
	    int actionType = ev.getAction();
	    //Toast toast;
	    
	    switch (actionType) {
	    	case MotionEvent.ACTION_UP:
//	    		if(!premik) {
	    			Projection proj = mapa.getProjection();
	    			GeoPoint loc = proj.fromPixels((int)ev.getX(), (int)ev.getY()); 
	    			longitud = new Double(loc.getLongitudeE6());
	    			latitud = new Double(loc.getLatitudeE6());
	           
//	    			toast = Toast.makeText(getApplicationContext(), "Longitud: " + longitud +" Latitud: " + latitud, Toast.LENGTH_LONG);
//	    			toast.show();
	    			longitudText.setText("Longitud: " + longitud.toString());
	    			latitudText.setText("Latitud: " + latitud.toString());
	    			
	    			
	    			
	    			itemizedoverlay = new MyItemizedOverlay(this, marker);
	    			itemizedoverlay.addPoint(loc, "Punto");
	    			
	    			mapOverlays.clear();
	    			mapOverlays.add(itemizedoverlay);
//	    		}
//	    	case MotionEvent.ACTION_DOWN:
//	    		premik = false;
//	    	case MotionEvent.ACTION_MOVE:
//	    		premik = true;

	    }

	    return super.dispatchTouchEvent(ev);
	}
	
	
	public void fijarCoordenadas(View v) {
		Intent resultIntent;

		resultIntent = new Intent();
		resultIntent.putExtra("ln", longitud);
		resultIntent.putExtra("lt", latitud);
		setResult(Activity.RESULT_OK, resultIntent);
		finish();
	}


}
